
package com.htd.htdapmservice.task;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.htd.htdapmcommon.enums.AlarmsStatusEnum;
import com.htd.htdapmdmo.OneAlertAlarmsRecordInfo;
import com.htd.htdapmservice.dao.AlarmsInfoDao;
import com.htd.htdapmservice.dao.OneAlertAlarmsRecordDao;
import com.taobao.pamirs.schedule.IScheduleTaskDealMulti;
import com.taobao.pamirs.schedule.TaskItemDefine;


@Component("syncAlarmInfoStatusTask")
public class SyncAlarmInfoStatusTask implements IScheduleTaskDealMulti<OneAlertAlarmsRecordInfo> {
    private static final Logger logger = LoggerFactory.getLogger(SyncAlarmInfoStatusTask.class);

    @Autowired
    private AlarmsInfoDao alarmsInfoDao;
    
    @Autowired 
    private OneAlertAlarmsRecordDao oneAlertAlarmsRecordDao;
    /**
     *
     * @param oneAlertAlarmsRecordInfoList
     * @param ownSign
     * @return
     * @throws Exception
     */
    @Override
    public boolean execute(OneAlertAlarmsRecordInfo[] oneAlertAlarmsRecordInfoList, String ownSign) throws Exception {
        logger.info("SyncAlarmInfoStatusTask execute Entrance, itemBrands : {}, ownSign : {}", JSONObject.toJSONString(oneAlertAlarmsRecordInfoList), JSONObject.toJSONString(ownSign));
        boolean result = true;
        try {
        	  if (oneAlertAlarmsRecordInfoList!=null &&oneAlertAlarmsRecordInfoList.length>0) {
        		  alarmsInfoDao.updateAlarmInfoStatus(Arrays.asList(oneAlertAlarmsRecordInfoList));
        	  }
          
        }catch (Exception e) {
            logger.error("SyncAlarmInfoStatusTask execute error", e);
            result = false;
        }
        return result;
    }


    /*
     * 选择任务数据。
     * @param taskParameter 任务参数
     * @param ownSign 当前环境名称
     * @param taskQueueNum 当前任务类型的任务队列数量
     * @param taskItemList 当前调度服务器，分配到的可处理队列
     * @param eachFetchDataNum 每次获取数据的数量
     * @return
     * @throws Exception
     */

    @Override
    public List<OneAlertAlarmsRecordInfo> selectTasks(String taskParameter, String ownSign, int taskQueueNum, List<TaskItemDefine> taskItemList, int eachFetchDataNum) throws Exception {
        logger.info("syncAlarmStatusTask selectTasks Entrance, taskParameter : {}, ownSign : {}, taskQueueNum : {}, taskItemList : {}," +
                "eachFetchDataNum : {}", new Object[]{JSONObject.toJSONString(ownSign), JSONObject.toJSONString(taskParameter),
                JSONObject.toJSONString(taskQueueNum), JSONObject.toJSONString(taskItemList), JSONObject.toJSONString(eachFetchDataNum)});
        List<OneAlertAlarmsRecordInfo> oneAlertAlarmsRecordInfoList =new ArrayList<OneAlertAlarmsRecordInfo>();
        try{
            List<String> taskIdList = new ArrayList<String>();
            Map<String, Object> paramMap = new HashMap<>();
            if (taskItemList != null && taskItemList.size() > 0) {
                for (TaskItemDefine taskItem : taskItemList) {
                    taskIdList.add(taskItem.getTaskItemId());
                }
                // 根据id和队列数取摩，在taskIdList中则被该任务查询并执行
                paramMap.put("taskQueueNum", taskQueueNum);
                paramMap.put("taskIdList", taskIdList);
                // 默认查询数量
                paramMap.put("rows", eachFetchDataNum > 0 ? eachFetchDataNum : 20);
                oneAlertAlarmsRecordInfoList = oneAlertAlarmsRecordDao.getListByStatus(AlarmsStatusEnum.ACTIVE.getCode());
            }
        } catch (Exception e){
            logger.error("SyncAlarmInfoStatusTask selectTasks error:", e);
        }
        return oneAlertAlarmsRecordInfoList;
      
    }

    @Override
    public Comparator getComparator() {
        return null;
    }



}
